package com.jvm123.blog.servlet

import javax.servlet.AsyncContext
import javax.servlet.ServletException
import javax.servlet.annotation.WebServlet
import javax.servlet.http.HttpServlet
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse

/**
 *
 * @author yawn http://jvm123.com
 *     2019/11/4 11:20
 */
@WebServlet(
        name = "testServlet",
        urlPatterns = "/servlet/test",
        asyncSupported = true
)
class TestServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        AsyncContext asyncContext = req.startAsync()
        asyncContext.start(new Runnable() {
            @Override
            void run() {
                doRequest(req, resp)
                asyncContext.complete()
            }
        })
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doRequest(req, resp)
    }

    private void doRequest(HttpServletRequest req, HttpServletResponse resp) {
        resp.getOutputStream().print("这是一个原生的 HttpServlet！")
    }
}
